package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.DoctorAssessBean;
import com.hxkcloud.patient.viewholders.DoctorEvalutionViewHolder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-10-09.
 * Description:患者端医生评价列表
 */
public class DoctorEvalutionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<DoctorAssessBean> datas;
    private LayoutInflater mInflater;
    private onEvalutionVoiceListener listener;

    public DoctorEvalutionAdapter(Context mContext, List<DoctorAssessBean> datas, onEvalutionVoiceListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        mInflater = LayoutInflater.from(mContext);
        this.listener = listener;
    }

    public void setData(List<DoctorAssessBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_doctorevalution, null);
        DoctorEvalutionViewHolder holder = new DoctorEvalutionViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DoctorEvalutionViewHolder viewHolder = (DoctorEvalutionViewHolder) holder;
        DoctorAssessBean data = datas.get(position);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(data.getTime());
        } catch (Exception e) {
        }
        String dateString = formatter.format(date);
        viewHolder.tv_time.setText(dateString);
        viewHolder.tv_doctor.setText(data.getDoctorName().substring(0, 1) + "医生");
        if (!TextUtils.isEmpty(data.getContent())) {
            viewHolder.tv_content.setVisibility(View.VISIBLE);
            viewHolder.tv_content.setText(data.getContent());
        } else {
            viewHolder.tv_content.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(data.getVoiceUrl())) {
            viewHolder.ll_voice.setVisibility(View.VISIBLE);
            viewHolder.tv_voice.setText("‘’" + data.getVoiceLength());

            viewHolder.ll_voice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onVoice(position);
                }
            });

            if (data.isVoicePlaying()) {
                AnimationDrawable animDrawable = (AnimationDrawable) ContextCompat.getDrawable(mContext, R.anim.anim_voice);
                animDrawable.start();
                viewHolder.iv_voice.setImageDrawable(animDrawable);

            } else {
                viewHolder.iv_voice.setImageResource(R.drawable.report_voice);
            }
        } else {
            viewHolder.ll_voice.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface onEvalutionVoiceListener {
        void onVoice(int position);
    }
}
